package com.pe1n.yangtaopai.view.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyViewPagerAdapter;
import com.pe1n.yangtaopai.base.BaseFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.BaihuoFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.JiankangFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.JingxuanFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.MeizhuangFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.MuyingFragment;
import com.pe1n.yangtaopai.page.home.view.fragment.XiangbaoFragment;
import com.pe1n.yangtaopai.page.search.view.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

public class HomePageFragment extends BaseFragment {

    private Context mContext = getContext();
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> mList = new ArrayList<>();
    private String[] TABS;
    private JingxuanFragment jingxuanFragment;
    private MeizhuangFragment meizhuangFragment;
    private MuyingFragment muyingFragment;
    private JiankangFragment jiankangFragment;
    private XiangbaoFragment xiangbaoFragment;
    private BaihuoFragment baihuoFragment;
    private MyViewPagerAdapter adapter;
    private ImageView imageViewSearch;


    public HomePageFragment() {
        // Required empty public constructor
    }

    public static HomePageFragment newInstance() {
        HomePageFragment fragment = new HomePageFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_page, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = (TabLayout) view.findViewById(R.id.tab_home);
        viewPager = (ViewPager) view.findViewById(R.id.vp_home);
        imageViewSearch = (ImageView) view.findViewById(R.id.iv_serach);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TABS = mContext.getResources().getStringArray(R.array.tab_list);

        initData();

        initView();

    }

    private void initView() {

        adapter = new MyViewPagerAdapter(getChildFragmentManager(),TABS,mList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(5);
        tabLayout.setupWithViewPager(viewPager);
        imageViewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                mContext.startActivity(intent);
            }
        });

    }

    private void initData() {

        jingxuanFragment = new JingxuanFragment();
        meizhuangFragment = new MeizhuangFragment();
        muyingFragment = new MuyingFragment();
        jiankangFragment = new JiankangFragment();
        xiangbaoFragment = new XiangbaoFragment();
        baihuoFragment = new BaihuoFragment();

        mList.add(jingxuanFragment);
        mList.add(meizhuangFragment);
        mList.add(muyingFragment);
        mList.add(jiankangFragment);
        mList.add(xiangbaoFragment);
        mList.add(baihuoFragment);

    }

}
